package cn.zktr.Personnel.model.service.zhouxiong;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.zktr.Personnel.common.DateListString;
import cn.zktr.Personnel.common.TimeStamp;
import cn.zktr.Personnel.model.dao.zhouxiong.*;
import cn.zktr.Personnel.model.dao.zhouyongcong.*;
import cn.zktr.Personnel.model.mappers.zhouxiong.*;
import cn.zktr.Personnel.model.pojos.*;
import cn.zktr.Personnel.model.service.zhouyongcong.*;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitsaveService;

@Service
@Transactional(rollbackOn=Exception.class)
public class NoimpService {

	@Autowired
	private CwaQtsMapper qtsM;
	@Autowired
	private CwaQtsDAO qtsD;
	@Autowired
	private CwaFidMapper fidM;
	@Autowired
	private CwaSstMapper sstM;
	@Autowired
	private CwaSstDAO sstD;
	@Autowired
	private CwaResMapper resM;
	@Autowired
	private findService fs;
	@Autowired
	private KaoQinKaService kqk;
	@Autowired
	private PmsEmpDao emp;
	@Autowired
	private CwaRsnDAO rsnD;
	@Autowired
	private CwaKprDAO kprD;
	@Autowired
	private CwaKprMapper kprM;
	@Autowired
	private CwaCwasDAO cwaD;

	@Autowired
	private monitsaveService mss;
	@Autowired
	private boolService bs;
	
	/**加班0  补班1
	 * @param QTSREM
	 * @return
	 */
	public JSONArray seleBuJiaBan(Integer qtsstat) {
		try {
			JSONArray seleBuJiaBan = qtsM.seleBuJiaBan(qtsstat);
//			System.err.println(seleBuJiaBan.size());
			return seleBuJiaBan;
		} catch (Exception e) {
			return null;
		}
	}
	
	/**根据时间 人 查询这天是否有加补班记录
	 * @param QTSTIME
	 * @param EMPID
	 * @return
	 */
	public Integer seleJiaBanSFCZ(String QTSTIME,Integer EMPID) {
		try {
			return qtsM.seleJiaBanSFCZ(QTSTIME, EMPID).size();
		} catch (Exception e) {
			return 0;
		}
	}
	
	/**全部加班数据
	 * @param DATE
	 * @return
	 */
	public JSONArray seleJiaBanShuJu(String date) {
		JSONArray dates = new JSONArray();
		try {
			JSONArray ry = resM.KaoQinShuJuQianZhi_KaoQinRenYuan();
			for (int i = 0; i < ry.size(); i++) {
				JSONArray lb = resM.selKaoQinShiJieLieBiao(ry.getJSONObject(i).getInteger("EMPID"),date);
				for (int j = 0; j < lb.size(); j++) {
					JSONObject seleKaoQinShuJu = resM.seleJiaBanShuJu(ry.getJSONObject(i).getInteger("EMPID"),0);
					Timestamp RESMINTIME = resM.KaoQinShuJuQianZhi1_RESMINTIME(ry.getJSONObject(i).getInteger("EMPID"), lb.getJSONObject(j).getString("RESTIME"), 0);
					Timestamp RESMAXTIME = resM.KaoQinShuJuQianZhiQianZhi2_RESMAXTIME(ry.getJSONObject(i).getInteger("EMPID"), lb.getJSONObject(j).getString("RESTIME"), 0);
					Integer RESTIME = resM.KaoQinShuJuQianZhiQianZhi3_RESTIME(ry.getJSONObject(i).getInteger("EMPID"), lb.getJSONObject(j).getString("RESTIME"), 0);
					if(RESMINTIME!=null && RESMAXTIME!=null && RESTIME!=null ) {
						seleKaoQinShuJu.put("RESMINTIME", RESMINTIME);
						seleKaoQinShuJu.put("RESMAXTIME", RESMAXTIME);
						seleKaoQinShuJu.put("RESTIME", RESTIME);
						dates.add(seleKaoQinShuJu);
					}
				}
			}
			return dates;
			
		} catch (Exception e) {
			e.printStackTrace();
			return dates;
		}
	}
	
	/**员工 加班数据
	 * @param EMPID
	 * @param DATE
	 * @return
	 */
	public List<JSONObject> seleJiaBanShuJuEmpid(Integer EMPID,String DATE) {
		List<JSONObject> jiabanlist = new ArrayList<>();
		try {
			JSONArray shijian = resM.selKaoQinShiJieLieBiao(EMPID,DATE);
			System.err.println("kao"+shijian.size());
			for (int i = 0; i < shijian.size(); i++) {
				JSONObject shuju = resM.seleJiaBanShuJu(EMPID, 0);
				Timestamp RESMINTIME = resM.KaoQinShuJuQianZhi1_RESMINTIME(EMPID, shijian.getJSONObject(i).getString("RESTIME"), 0);
				Timestamp RESMAXTIME = resM.KaoQinShuJuQianZhiQianZhi2_RESMAXTIME(EMPID, shijian.getJSONObject(i).getString("RESTIME"), 0);
				Integer RESTIME = resM.KaoQinShuJuQianZhiQianZhi3_RESTIME(EMPID, shijian.getJSONObject(i).getString("RESTIME"), 0);
				if(RESMINTIME!=null && RESMAXTIME!=null && RESTIME!=null ) {
					shuju.put("RESMINTIME", RESMINTIME);
					shuju.put("RESMAXTIME", RESMAXTIME);
					shuju.put("RESTIME", RESTIME);
					jiabanlist.add(shuju);
				}
			}
			System.err.println("kao2"+jiabanlist.size());
			return jiabanlist;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public String updateJiaBan(Integer EMPID,Integer RSNID,String QTSTIME,String QTSREM) {
		try {
			if(qtsM.seleJiaBanQianZhi(EMPID, QTSTIME).size()>0)
				return "-1";
			PerCwaQts qts = new PerCwaQts();
			PerCwaCwas cwa = kqk.findByEmpid(EMPID);
			qts.setQtsid(bs.bool_Qts());
			qts.setPerPmsEmp(emp.findById(EMPID).get());
			qts.setPerCwaCwa(cwa);
			qts.setPerCwaRsn(rsnD.findById(RSNID).get());
			qts.setQtstime(new TimeStamp().valueOf(QTSTIME));
			qts.setQtsrem(QTSREM);
			qts.setQtsstat("0");
			mss.save_09(EMPID, qts);
			return "0";
		} catch (Exception e) {
			return "0";
		}
	}
	
	
	
	/**外勤
	 * @return
	 */
	public JSONArray seleWaiQin() {
		try {
			return fidM.seleWaiQin();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**调班
	 * @return
	 */
	public JSONArray seleTiaoBan() {
		try {
			return sstM.seleTiaoBan();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**调班
	 * @param empid
	 * @param cwaid
	 * @param cwasid
	 */
	public void updateCwa(Integer empid,Integer cwaid,Integer cwasid) {
		try {
			PerCwaKpr kpr = kprM.selectTBRY(cwaid, empid);
			kpr.setPerCwaCwa(cwaD.findById(cwasid).get());
			kpr.setPerPmsEmp(emp.findById(empid).get());
			kpr.setKprtime(new TimeStamp().getNetworkTime());
			kprD.save(kpr);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	/**调班记录
	 * @param empid
	 * @param cwaid
	 * @param cwasid
	 * @param rsnid
	 * @param sstrem
	 */
	public void updateTB(Integer empid,Integer cwaid,Integer cwasid,Integer rsnid,Integer sstrem) {
		try {
			PerCwaSst sst = new PerCwaSst();
			sst.setPerPmsEmp(emp.findById(empid).get());
			sst.setPerCwaCwa1(cwaD.findById(cwaid).get());
			sst.setPerCwaCwa2(cwaD.findById(cwasid).get());
			sst.setSsttime(new TimeStamp().getNetworkTime());
			sst.setPerCwaRsn(rsnD.findById(rsnid).get());
			sstD.save(sst);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
